package com.ailink.basic.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {

    private static Object lockObject = new Object();

    public static void main(String[] args) throws InterruptedException {
        synchronized(lockObject) {
            lockObject.wait(1000);
        }
        System.out.println("LockSupport.unpark(Thread.currentThread())");
        LockSupport.unpark(Thread.currentThread());
        LockSupport.parkNanos(1000 * 1000 * 1000);
        System.out.println("LockSupport.parkNanos(1000 * 1000 * 1000)");

        ReentrantLock lock = new ReentrantLock();
        List<Thread> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(() -> {
                System.out.println(Thread.currentThread());
                lock.lock();
                System.out.println(Thread.currentThread() + " lock");
                try {
                    Thread.sleep(1 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.unlock();
                System.out.println(Thread.currentThread() + " unlock");
            });
            list.add(thread);
            thread.start();
        }

        System.out.println(Thread.currentThread());
    }
}
